package com.hyzx.qbasic.admin.controller;

import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.CommonAttachmentPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.DeleteAttachmentReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetAttachmentListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.model.vo.res.UploadFileResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.ICommonFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2018/8/10
 */


@Api(tags = "文件服务", description = "CommonFileController")
@RestController
public class CommonFileController extends BaseController {

    @Autowired
    private ICommonFileService commonFileService;

    @ApiIgnore
    @SysLog("上传图片")
    @PostMapping("/uploadImage")
    public UploadFileResModel uploadImage(String businessId,
                                          @RequestParam(defaultValue = "0") int priority,
                                          @RequestParam("upload") MultipartFile multipartFile) {
        return commonFileService.uploadImage(businessId, priority, multipartFile);
    }

    @ApiIgnore
    @SysLog("上传附件")
    @PostMapping("/uploadFile")
    public UploadFileResModel uploadFile(String businessId,
                                         @RequestParam(defaultValue = "0") int priority,
                                         @RequestParam("upload") MultipartFile multipartFile) {
        return commonFileService.uploadFile(businessId, priority, multipartFile);
    }

    @ApiOperation("删除附件")
    @PostMapping("/deleteAttachment")
    @ResponseBody
    public BaseResModel<BaseOperationRes> deleteAttachment(@Valid @RequestBody DeleteAttachmentReq req, BindingResult result) {
        return BaseResUtils.success(commonFileService.deleteAttachment(req.getFileId(), req.getContentId()));
    }

    @ApiOperation("删除图片")
    @PostMapping("/deleteImage")
    @ResponseBody
    public BaseResModel<BaseOperationRes> deleteImage(@Valid @RequestBody DeleteAttachmentReq req, BindingResult result) {
        return BaseResUtils.success(commonFileService.deleteImage(req.getFileId(), req.getContentId()));
    }

    @ApiOperation("获取附件列表")
    @GetMapping("/getAttachmentList")
    @ResponseBody
    public BaseResModel<List<CommonAttachmentPO>> getAttachmentList(@Valid GetAttachmentListReq req, BindingResult result) {
        return BaseResUtils.success(commonFileService.getAttachmentList(req.getBusinessId()));
    }
}
